Análise de discriminante: linear e quadrático (LDA/QDA)
Recapitulando
- Teorema (Classificador de Bayes): A função \(g: \mathbb{R}^p \to \mathcal{C}\) que minimiza o risco \[R(g) = \mathbb{E}[\mathbb{I}(Y \neq g(\mathbf{X}))] = \mathbb{P}(Y \neq g(\mathbf{X}))\] é o classificador de Bayes, dado por \[g(\mathbf{x}) = \mathop{\mathrm{argmax}}_{d \in \mathcal{C}} \mathbb{P}(Y = d | \mathbf{X} = \mathbf{x})\]
Recapitulando
- Se \(\mathbf{X}\) é contínuo com densidade \(q(\mathbf{x})\), temos que: \[\mathbb{P}(Y = d | \mathbf{X} = \mathbf{x}) \propto q(\mathbf{x} | Y = d)\mathbb{P}(Y = d)\]
- Análise de discriminante: Não assumir independência nas componentes de \(\mathbf{X} | (Y = d)\), mas que seguem uma normal multivariada mais geral
A distribuição normal multivariada
\[\begin{align*} \mathbf{Z} \sim &N(\boldsymbol{\mu}, \boldsymbol{\Sigma}) \iff \\ &p(\mathbf{z}) = \frac{1}{\sqrt{(2 \pi)^p \det(\boldsymbol{\Sigma})}} \times \\ &\quad\quad\quad\exp\left(-\frac{1}{2} (\mathbf{z} - \boldsymbol{\mu})^T \boldsymbol{\Sigma}^{-1} (\mathbf{z} - \boldsymbol{\mu})\right) \end{align*}\] onde \(\mathbf{z}, \boldsymbol{\mu} \in \mathbb{R}^p\) e \(\boldsymbol{\Sigma}\) é uma matriz \(p \times p\) simétrica positiva-definida
A distribuição normal multivariada
![]()
[Wikipedia] Dados de uma normal bivariada com \(\boldsymbol{\mu} = \begin{bmatrix} 0 \\ 0\end{bmatrix}\) e \(\boldsymbol{\Sigma} = \begin{bmatrix} 1 & 3/5 \\ 3/5 & 2\end{bmatrix}\).
Análise de discriminante linear (LDA)
- Cada \(\mathbf{X} | (Y = d)\) pode ter médias distintas porém as classes compartilham a mesma matriz de covariância: \[\mathbf{X} | (Y = d) \sim N(\boldsymbol{\mu}_d, \boldsymbol{\Sigma})\]
- Parâmetros estimados por máxima verossimilhança: \[\begin{align*} \widehat{\boldsymbol{\mu}}_{d} &= \frac{1}{|\mathcal{C}_d|} \sum_{k \in \mathcal{C}_d} \mathbf{x}_{k} \\ \widehat{\boldsymbol{\Sigma}} &= \frac{1}{n} \sum_{d \in \mathcal{C}} \sum_{k \in \mathcal{C}_d} (\mathbf{x}_k - \widehat{\boldsymbol{\mu}}_d)(\mathbf{x}_k - \widehat{\boldsymbol{\mu}}_d)^T \end{align*}\]
Análise de discriminante linear (LDA)
- Parâmetros estimados por máxima verossimilhança: \[\begin{align*} \widehat{\boldsymbol{\mu}}_{d} &= \frac{1}{|\mathcal{C}_d|} \sum_{k \in \mathcal{C}_d} \mathbf{x}_{k} \\ \widehat{\boldsymbol{\Sigma}} &= \frac{1}{n} \sum_{d \in \mathcal{C}} \sum_{k \in \mathcal{C}_d} (\mathbf{x}_k - \widehat{\boldsymbol{\mu}}_d)(\mathbf{x}_k - \widehat{\boldsymbol{\mu}}_d)^T \end{align*}\]
- \(\mathcal{C}_d\) são todas as observações de treinamento da classe \(d\)
- \(\mathcal{C}\) são todas as classes
Análise de discriminante linear (LDA)
- Podemos então considerar o classificador plug-in: \[\begin{align*} g(\mathbf{x}) &= \mathrm{argmax}_{d \in \mathcal{C}} \widehat{\mathbb{P}}(Y = d | \mathbf{X} = \mathbf{x}) \\ &= \mathrm{argmax}_{d \in \mathcal{C}} \widehat{q}(\mathbf{x} | Y = d)\widehat{\mathbb{P}}(Y = d) \\ &= \mathrm{argmax}_{d \in \mathcal{C}} \widehat{\delta}_d(\mathbf{x}) \end{align*}\]
- \(\widehat{\delta}_d\) são as funções discriminante (linear em \(\mathbf{x}\)!): \[\widehat{\delta}_d(\mathbf{x}) = \widehat{\boldsymbol{\mu}}_d^T \widehat{\boldsymbol{\Sigma}}^{-1} \mathbf{x} - \frac{1}{2}\widehat{\boldsymbol{\mu}}_d^T \widehat{\boldsymbol{\Sigma}}^{-1} \widehat{\boldsymbol{\mu}}_d + \ln(\widehat{\mathbb{P}}(Y = d))\]
Análise de discriminante linear (LDA)
- \(\widehat{\delta}_d(\mathbf{x})\) lineares em \(\mathbf{x}\)
- As fronteiras de decisão \(\{\mathbf{x} | \delta_d(\mathbf{x}) = \delta_c(\mathbf{x})\}\) são hiperlanos em \(\mathbb{R}^p\)
- Vejamos uma ilustração
Análise de discriminante linear (LDA)
![]()
(Figura 4.6 de [ITSL]) Exemplo com três classes, dados normalmente distribuídos em cada classe, com \(p = 2\). Esquerda: Elipses que contém 95% da probabilidade para cada uma das três classes e linhas pontilhadas representam as fronteiras de decisão do classificador de Bayes; Direita: 20 observações de cada classe e fronteiras de decisão do classificador LDA (linha contínua).
Análise de discriminante quadrático (QDA)
- \(\mathbf{X} | (Y = d)\) tem sua própria média e matriz de covariância: \[\mathbf{X} | (Y = d) \sim N(\boldsymbol{\mu}_d, \boldsymbol{\Sigma}_d)\]
- Parâmetros estimados por máxima verossimilhança: \[\begin{align*} \widehat{\boldsymbol{\mu}}_{d} &= \frac{1}{|\mathcal{C}_d|} \sum_{k \in \mathcal{C}_d} \mathbf{x}_{k} \\ \widehat{\boldsymbol{\Sigma}}_d &= \frac{1}{|\mathcal{C}_d|} \sum_{k \in \mathcal{C}_d} (\mathbf{x}_k - \widehat{\boldsymbol{\mu}}_d)(\mathbf{x}_k - \widehat{\boldsymbol{\mu}}_d)^T \end{align*}\]
Análise de discriminante quadrático
- Podemos então considerar o classificador plug-in: \[\begin{align*} g(\mathbf{x}) &= \mathrm{argmax}_{d \in \mathcal{C}} \widehat{\mathbb{P}}(Y = d | \mathbf{X} = \mathbf{x}) \\ &= \mathrm{argmax}_{d \in \mathcal{C}} \widehat{q}(\mathbf{x} | Y = d)\widehat{\mathbb{P}}(Y = d) \\ &= \mathrm{argmax}_{d \in \mathcal{C}} \widehat{\delta}_d(\mathbf{x}) \end{align*}\]
- \(\widehat{\delta}_d\) são as funções discriminante (quadráticas em \(\mathbf{x}\)!): \[\begin{align*} \widehat{\delta}_d(\mathbf{x}) &= -\frac{1}{2}\ln(\det(\widehat{\boldsymbol{\Sigma}}_d)) - \frac{1}{2} \mathbf{x}^T \widehat{\boldsymbol{\Sigma}}_d^{-1} \mathbf{x} \\ &+ \widehat{\boldsymbol{\mu}}_d^T \widehat{\boldsymbol{\Sigma}}_d^{-1} \mathbf{x} - \frac{1}{2}\widehat{\boldsymbol{\mu}}_d^T \widehat{\boldsymbol{\Sigma}}_d^{-1} \widehat{\boldsymbol{\mu}}_d + \ln(\widehat{\mathbb{P}}(Y = d)) \end{align*}\]
Análise de discriminante quadrático (QDA)
- \(\widehat{\delta}_d(\mathbf{x})\) quadráticas em \(\mathbf{x}\)
- As fronteiras de decisão \(\{\mathbf{x} | \delta_d(\mathbf{x}) = \delta_c(\mathbf{x})\}\) são superfícies quádricas em \(\mathbb{R}^p\)
- Vejamos uma ilustração
Análise de discriminante quadrático (QDA)
![]()
Figura 4.9 de [ITSL]) Fronteiras de decisão do classificador de Bayes (tracejado roxo), LDA (pontilhado preto) e QDA (linha verde contínua) com duas classes. Esquerda: \(\boldsymbol{\Sigma}_1 = \boldsymbol{\Sigma}_2\); direita: \(\boldsymbol{\Sigma}_1 \neq \boldsymbol{\Sigma}_2\).
LDA ou QDA: qual escolher?
![]()
Retirada da página do scikit-learn. Comparação entre LDA e QDA.
LDA ou QDA: qual escolher?
- \(K\) classes e \(p\) preditores
- LDA estima \(Kp + p(p + 1)/2\) parâmetros: \[\boldsymbol{\mu}_1, \dots, \boldsymbol{\mu}_K ~ \mathrm{ e } ~ \boldsymbol{\Sigma}\]
- QDA estima \(Kp + Kp(p + 1)/2\) parâmetros: \[\boldsymbol{\mu}_1, \dots, \boldsymbol{\mu}_K ~ \mathrm{ e } ~ \boldsymbol{\Sigma}_1, \dots, \boldsymbol{\Sigma}_K\]
LDA ou QDA: qual escolher?
- LDA estima menos parâmetros que QDA \(\implies\) menos flexível/maior viés
- Por outro lado, requer menos dados para seu treinamento \(\implies\) “menos sensível ao conjunto de dados”/menor variância
- QDA estima mais parâmetros que LDA \(\implies\) mais flexível/menor viés
- portanto requer mais dados para seu treinamento \(\implies\) “mais sensível ao conjunto de dados”/maior variância
LDA ou QDA: qual escolher?
- LDA pode ser melhor que QDA se “poucas” amostras de treinamento estão disponíveis
- QDA pode ser melhor se o conjunto de treinamento é “grande” ou a hipótese de \(\boldsymbol{\Sigma}\) constante é inverossímil